﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Nirvana
{
    /// <summary>
    /// 场景Tag标签设置脚本
    /// </summary>
    public static class SceneTagManager
    {
        public enum LabelIcon
        {
            None = -1,
            Gray,
            Blue,
            Teal,
            Green,
            Yellow,
            Orange,
            Red,
            Purple
        }

        public enum Icon
        {
            None =-1,
            //圆形
            CircleGray,
            CircleBlue,
            CircleTeal,
            CircleGreen,
            CircleYellow,
            CircleOrange,
            CircleRed,
            CirclePurple,
            //宝石状
            DiamondGray,
            DiamondBlue,
            DiamondTeal,
            DiamondGreen,
            DiamondYellow,
            DiamondOrange,
            DiamondRed,
            DiamondPurple
        }
        static GUIContent[] labelGroup,iconGroup;

        public static void SetIcon(GameObject go,LabelIcon label)
        {
            if (labelGroup == null) labelGroup = CreateIconContenGroup("sv_label_", "", 0, 8);
            if(label == LabelIcon.None)
            {
                RemoveIcon(go);
            }
            else
            {
                SetIconForObject(go, labelGroup[(int)label].image as Texture2D);
            }
        }

        public static void SetIcon(GameObject go, Icon icon)
        {
            if(iconGroup == null) iconGroup = CreateIconContenGroup("sv_icon_dot", "_pix16_gizmo", 0, 16);

            if (icon == Icon.None)
            {
                RemoveIcon(go);
            }
            else
            {
                SetIconForObject(go, iconGroup[(int)icon].image as Texture2D);
            }

        }

        private static GUIContent[] CreateIconContenGroup(string labelname,string flag,int start,int length)
        {
            var group = new GUIContent[length];
            for(int i = 0; i < length; i++)
            {
                string label = labelname + (start + i) + flag;
                group[i] = EditorGUIUtility.IconContent(label);
            }
            return group;
        }

        public static void RemoveIcon(GameObject go)
        {
            SetIconForObject(go, null);
        }

        private static void SetIconForObject(GameObject go, Texture2D texture)
        {
            var method = typeof(EditorGUIUtility).GetMethod("SetIconForObject", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
            method.Invoke(null,new object[] { go, texture });        
        }


    }

}
